package br.com.lavitstudio.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.File;

public class Character extends DoubleRectangle {
	public double fallingSpeedDefault = 1;
	public double fallingSpeed = fallingSpeedDefault;
	public double jumpingSpeed = 1;
	
	public static double movementSpeedWalking = 0.8;
	public static double movementSpeedRunning = 1.2;
	public static double movementSpeed = movementSpeedWalking;

	public int doubleHeight = 2;
	public int revisionY = 3;
	public int jumpingHeight = 30, jumpingCount = 0;
	public int animationFrame = 0, animationTime = 15;
	public int animation = 0;
	public int stX = -145;
	public int stY = -100;
	
	public static int animationFrameMaxWalking = 2;
	public static int animationFrameMaxRunning = 3;
	public static int animationFrameMaxResting = 0;
	public static int animationFrameMax = animationFrameMaxWalking;
	public static int playerLife    = 1010;
	public static int playerMana    = 1010;
	public static int playerStamina = 1010;
	public static int playerWeight  = 1010;
	
	public static int startX = ((Level.worldW * Tile.tileSize) / 2);
	public static int startY = 0;
	
	public boolean isJumping = false;
	public boolean isDesktop = false;
	
	public static boolean canMove = false;
	public static boolean isPlayerWalking = true;
	public static boolean isPlayerRunning = false;
	public static boolean isPlayerResting = false;
	public static boolean isPlayerSitting = false;
	public static boolean isPlayerWorking = false;
	public static boolean isPlayerAtack1 = false;
	public static boolean isPlayerAtack2 = false;
	public static boolean isPlayerAtack3 = false;
	public static boolean isPlayerAtack4 = false;
	public static boolean isPlayerAtack5 = false;
	public static boolean isPlayerAtack6 = false;
	public static boolean isPlayerAtack7 = false;
	

	private file_RW charRW = new file_RW();
	
	public Character() {
		File file = new File(Component.charFile);
		if(file.exists()){
			charRW.readCHAR();
		}
		
		int x = (Component.pixel.width / 2) - (Tile.tileSize /2) + stX + startX;
		int y = (Component.pixel.height / 2) - ((Tile.tileSize * doubleHeight) / 2) + stY + startY;
		
		setBounds(x, y, Tile.tileSize, (Tile.tileSize * doubleHeight));
		Component.sX += stX + startX;
		Component.sY += stY + startY;
		
		saveChar();
	}
	
	public void saveChar() {
		file_RW.writeCHAR((int) (Component.sX + 145), (int) (Component.sY + 100), playerLife, playerMana, playerStamina, playerWeight);
	}
	
	public void tick() {
		// Queda
		//if(!isJumping && !isCollidingWithBlock(new Point((int) x, (int) (y + height)), new Point((int) (x + width), (int) (y + height)))) { // Ninja jump
		if(!isJumping && !CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) (x + 2), (int) (y + height)), new Point((int) (x + width - 2), (int) (y + height)))) { // Normal jump
			fallingSpeed += 0.05; // Almenta a velocidade da queda
			y += fallingSpeed;
			Component.sY += fallingSpeed;
		} else {
			// Calcula o dano da queda
			if(fallingSpeed > 5){
				playerLife -= (int) (fallingSpeed * 5);
				
				// Salvar dados do personagem
				saveChar();
			}
			fallingSpeed = fallingSpeedDefault; // restaura valor padrao de queda
			
			if(Component.isJumping && !Inventory.isOpen){
				isJumping = true;
			}
		}
		
		// Verifica status da Stamina
		if(isPlayerRunning) {
			if(playerStamina < 300) {
				Component.isMoving = false;
				isPlayerWalking = true;
				isPlayerRunning = false;
				movementSpeed = movementSpeedWalking;
				animationFrameMax = animationFrameMaxWalking;
				playerStamina = 0;
			} else {
				playerStamina--;
			}
		} else {
			if(playerStamina < 1009) {
				playerStamina++;
			}
		}
		
		// Movimento caminhar
		if(Component.isMoving && !Inventory.isOpen){
			if(Component.dir == movementSpeed) {
				canMove = CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) (x + width), (int) y), new Point((int) (x + width), (int) (y + (height - 2))));
			} else if(Component.dir == -movementSpeed) {
				canMove = CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) x, (int) y), new Point((int) x, (int) (y + (height - 2))));
			}
			
			if(animationFrame >= animationTime) {
				if(animation > animationFrameMax) {
					animation = 1;
				}else {
					animation += 1;
				}
				
				animationFrame = 0;
				
				// Salvar dados do personagem
				saveChar();
				
			} else {
				animationFrame += 1;
			}
			
			if(!canMove) {
				x += Component.dir;
				Component.sX += Component.dir;
			}
		} else {
			animation = 0;
		}
		
		
		// Movimento do pulo
		if(isJumping) {
			//if(!isCollidingWithBlock(new Point((int) x, (int) y), new Point((int) (x + width), (int) y))) { // Ninja jump
			if(!CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) (x + 2), (int) y), new Point((int) (x + width - 2), (int) y))) { // Normal Jump
				if(jumpingCount >= jumpingHeight){
					isJumping = false;
					jumpingCount = 0;
				} else {
					y -= jumpingSpeed;
					Component.sY -= jumpingSpeed;
										
					jumpingCount += 1;
				}
			} else {
				isJumping = false;
				jumpingCount = 0;
			}
		}
	}
	
	public void render(Graphics g) {
		if(isDesktop) {
			g.drawImage(Tile.desktop, (int) (x - Component.sX) - 60, (int) (y - Component.sY) - 60, 140, 140, null);
		}
		
		if(isPlayerWalking) {
			if(Component.dir == movementSpeed) {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Walking[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Walking[1] * Tile.tileSize, (Tile.mob_player_Walking[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Walking[1] * Tile.tileSize + (int) height, null);
			} else {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Walking[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Walking[1] * Tile.tileSize, (Tile.mob_player_Walking[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Walking[1] * Tile.tileSize + (int) height, null);
			}
		}
		
		if(isPlayerRunning) {
			if(Component.dir == movementSpeed) {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Running[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Running[1] * Tile.tileSize, (Tile.mob_player_Running[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Running[1] * Tile.tileSize + (int) height, null);
			} else {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Running[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Running[1] * Tile.tileSize, (Tile.mob_player_Running[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Running[1] * Tile.tileSize + (int) height, null);
			}
		}
		
		if(isPlayerResting) {
			if(Component.dir == movementSpeed) {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Resting[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Resting[1] * Tile.tileSize, (Tile.mob_player_Resting[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Resting[1] * Tile.tileSize + (int) height, null);
			} else {
				g.drawImage(Tile.tile_char, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (Tile.mob_player_Resting[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, Tile.mob_player_Resting[1] * Tile.tileSize, (Tile.mob_player_Resting[0] * Tile.tileSize) + (Tile.tileSize * animation), Tile.mob_player_Resting[1] * Tile.tileSize + (int) height, null);
			}
		}
		
		// Barra Stamina
		if(!Inventory.isOpen) {
			// Life
			g.setColor(new Color(255, 0 ,0,150));
			g.fillRect(0,0, (playerLife / 25) - 10, 5);
			g.setColor(new Color(0,0,0));
			g.drawRect(0,0,30,5);

			// Mana
			g.setColor(new Color(0, 55 ,255,150));
			g.fillRect(30,0, (playerMana / 25) - 10, 5);
			g.setColor(new Color(0,0,0));
			g.drawRect(30,0,30,5);
			
			// Stamina
			g.setColor(new Color(32, 194 ,0,150));
			g.fillRect(60,0, (playerStamina / 25) - 10, 5);
			g.setColor(new Color(0,0,0));
			g.drawRect(60,0,30,5);
			
			// Weight
			g.setColor(new Color(255, 246 ,0,150));
			g.fillRect(90,0, (playerWeight / 25) - 10, 5);
			g.setColor(new Color(0,0,0));
			g.drawRect(90,0,30,5);
		}
	}
}
